﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using docmgr.BLL;

namespace docmgr.WinForm {

    /// <summary>
    /// 
    /// </summary>
    public partial class DocumentList : DockContent
    {

        #region 预置基本参数开始
        //权限项名称
        private string PowerItemName = "Document";
        //当前操作员       
        string OpManage = "YCSK";//Common.curUser.UserCode;

        //--表格结构
        private const string TableName = "Document";
        //--表格主键
        string ID;
                // 表的字段结构
        string[] tbfelids = new string[]{
		"ID,int,ID",
		"DocName,varchar,文件标题",
		"Remark,varchar,Remark",
		"RegDate,smalldatetime,处理日期",
		"UserID,int,责任人",
		"SysSerialNumber,int,编号",
		"DocSerialNumber,varchar,发文字号",
		"SecretLevel,varchar,密级",
		"DepartmentID,int,文件单位",
		"DocType,varchar,文件类型",
		"PageNumber,int,页数",
		"Status,varchar,状态",
		"Code,varchar,Code",
		"HandleUserID,int,持有人"};

        DateTimePicker dateBegin = new DateTimePicker();
        DateTimePicker dateEnd = new DateTimePicker();
        //-- 声明业务对象
        docmgr.BLL.Document bll;

        docmgr.BLL.User userbll;
        docmgr.BLL.Department depbll;
        //-- 创建数据源
        DataSet mainDs = new DataSet();
        DataTable mainDt = new DataTable();

        Hashtable usertable = new Hashtable();
        Hashtable deptable = new Hashtable();
        //查询条件
        string hidSQL = ""; 
        StringBuilder constsql = new StringBuilder();
        string fields = "doc.ID, 	doc.DocSerialNumber 字号,	doc.DocName 文件标题,	doc.RegDate 处理日期,	 	doc.SecretLevel 密级,	doc.DocType 处理类型,	doc.PageNumber 页数,	doc.OutSideDepartMent 来文单位,	doc.Portion 份数,	dep.Name 发文单位,	u.Name 责任人,	dir.Name 文件类别,	doc.Remark 备注";

        string fields1 = "doc.ID,	doc.DocSerialNumber 字号,   doc.DocName 文件标题,	doc.RegDate 处理日期,	 		doc.SecretLevel 密级,	doc.DocType 处理类型,	doc.PageNumber 页数,	doc.OutSideDepartMent 来文单位,	doc.Portion 份数,	dep.Name 发文单位,	u.Name 责任人,	dir.Name 文件类别,	doc.Remark 备注";

        string fields2 = "doc.ID,	doc.DocSerialNumber 收文字号, doc.DocName 文件标题,	doc.RegDate 处理日期,	 		doc.SecretLevel 密级,	 	doc.PageNumber 页数,	doc.OutSideDepartMent 来文单位,	doc.Portion 份数,	 u.Name 责任人,	dir.Name 文件类别,	doc.Remark 备注";

        string fields3 = "doc.ID,	doc.DocSerialNumber 发文字号, doc.DocName 文件标题,	doc.RegDate 处理日期,	 		doc.SecretLevel 密级,	 doc.PageNumber 页数,	 	doc.Portion 份数,	dep.Name 发文单位,	u.Name 责任人,	dir.Name 文件类别,	doc.Remark 备注";

        
        //窗体初始化结束标志
        bool IsWinInitOver = false;
        #endregion

        #region 构造函数 、Load()、Shown()事件
        public DocumentList() {
            InitializeComponent();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

            initWindow();
        }

        private void DocumentList_Load(object sender,EventArgs e) {
            //--重新绑定表格数据源
            //BindGrid(null);
            int year =Convert.ToInt32(DateTime.Now.ToString().Substring(0, 4));
            TreeNode node = new TreeNode("所有文件");
            treeView1.Nodes.Add(node);
            for (int i = 0; i < 6;i++ )
            {
                treeView1.SelectedNode = node;
                TreeNode Node1=null;
                if (i != 5)
                    Node1 = new TreeNode("" + (year - i));
                else
                    Node1 = new TreeNode("其他年份");
                treeView1.SelectedNode.Nodes.Add(Node1);
                treeView1.SelectedNode = Node1;
                treeView1.SelectedNode.Nodes.Add("SW", "收文");
                treeView1.SelectedNode.Nodes.Add("FW", "发文");
                treeView1.SelectedNode.Nodes.Add("CPJ", "呈批件");
                treeView1.SelectedNode.Nodes.Add("HAN", "函");
                treeView1.SelectedNode.Nodes.Add("BTWJ", "白头文件");
                treeView1.SelectedNode.Nodes.Add("HT", "合同");

            }
            ///treeView1.ExpandAll();
  
            BindDataWithPage(1);
        }
        private void DocumentList_Shown(object sender,EventArgs e) {
            //窗体初始化结束标志
            IsWinInitOver = true;
        }
        #endregion

        #region 自定义初始化窗体 initWindow

        public void initWindow() {
            //--插入选择日期范围的日期控件

            
            dateBegin.Width = dateEnd.Width = 110;           
            toolStrip1.Items.Insert(2, new ToolStripControlHost(dateBegin, "dateBegin"));
            toolStrip1.Items.Insert(4, new ToolStripControlHost(dateEnd, "dateEnd"));
           
             //创建业务对象
            bll = new docmgr.BLL.Document();
            userbll = new User();
            depbll = new Department();

        }

        void bind(DataTable dt,Hashtable ht)
        {
            for(int i=0;i<dt.Rows.Count;i++)
            {
                ht.Add(dt.Rows[i]["ID"], dt.Rows[i]["Name"]);
            }
        }

        //设置窗体快捷键
        private void form_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
            if (!e.Control)
                return;
            switch (e.KeyCode) {
                case Keys.N://新建
                    this.tsbtnNew_Click(this.tsbtnNew, null);
                    break;
                case Keys.S://搜索
                    //this.tsbtnSave_Click(this.tsbtnSave, null);
                    break;
                case Keys.D://删除
                    //this.tsbtnDel_Click(this.tsbtnDel, null);
                    break;
		        case Keys.W://关闭窗口
                    //this.tsbtnClose_Click(this.tsbtnDel, null);
                    break;
                case Keys.H://显示帮助
                    this.tsbtnHelp_Click(this.tsbtnHelp, null);
                    break;
            }
        }
        #endregion       

        #region -- 主工具栏操作 新建、保存、删除、显示帮助
        //打开新建窗口
        private void tsbtnNew_Click(object sender, EventArgs e) {
            //if (!Common.getUserAccess(this.PowerItemName, opType.bDelete)) {
            //    MessageBox.Show(this, "没有操作权限", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return;
            //}
            /*using(docmgr.WinForm.DocumentForm frm = new docmgr.WinForm.DocumentForm()) {
                frm.WindowState = FormWindowState.Normal;
                frm.StartPosition = FormStartPosition.CenterScreen;
                if(frm.ShowDialog() == DialogResult.OK) {
                    BindGrid(null);
                }
            }*/

            using (AddDocument addDocument = new AddDocument())
            {
                addDocument.WindowState = FormWindowState.Normal;
                addDocument.StartPosition = FormStartPosition.CenterScreen;
                if (addDocument.ShowDialog() == DialogResult.OK)
                {

                }
                BindDataWithPage(allenPage1.PageIndex);
                //BindGrid(null);
            }
        }
        //保存列表
        protected void tsbtnSave_Click(object sender, EventArgs e) {
            //if (!Common.getUserAccess(this.PowerItemName, opType.bEdit)) {
            //    MessageBox.Show(this, "没有操作权限", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return;
            //}          
            //业务处理       
            SaveGrid();
        }

        //删除当前行记录
        private void tsbtnDel_Click(object sender, EventArgs e) {
            //if (!Common.getUserAccess(this.PowerItemName, opType.bDelete)) {
            //    MessageBox.Show(this, "没有操作权限", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return;
            //}
            if (this.dataGridView1.CurrentRow == null)
                return;
            string key = this.dataGridView1.CurrentRow.Cells["ColumnID"].Value.ToString();
            if (DialogResult.OK == MessageBox.Show(this, "确定要删除该条记录？", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)) {
                bll.DeleteByKey(key);
                //BindGrid(null);
                BindDataWithPage(allenPage1.PageIndex);
            }
        }
        //搜索查询
        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            string condition = "";//在这里设置查询条件,如 "title like '%good%'",为空就是查询所有记录
            //BindGrid(condition);//重新绑定表格
            if (dateBegin.Text != dateEnd.Text)
                condition += String.Format(" RegDate BETWEEN '{0}' and '{1}' ", Convert.ToDateTime(dateBegin.Text), Convert.ToDateTime(dateEnd.Text));
            else
                condition += "1=1";
            if (tsTBoxKey.Text!="")
            {
                /**全文搜索，就酱紫吧*/
                condition += String.Format(" and  (DocName like '%{0}%' or doc.Remark like '%{0}%' or u.name like '%{0}%'   or dep.name like '%{0}%'  or doc.DocSerialNumber like  '%{0}%'  or doc.SysSerialNumber like  '%{0}%' ) ", tsTBoxKey.Text);
                //condition += String.Format(" and  (DocName like '%{0}%' or doc.Remark like '%{0}%') ", tsTBoxKey.Text);
            }

            hidSQL = condition;
            BindDataWithPage(1);//, hidSQL);
            allenPage1.Refresh();

        }
        //关闭窗口
        private void tsbtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //显示帮助
        private void tsbtnHelp_Click(object sender, EventArgs e) {
            BindDataWithPage(1);//, hidSQL);
            allenPage1.Refresh();
        }
        #endregion

        #region -- 主要业务  dataGridView表格相关操作
        /// <summary>
        /// 根据查询条件绑定表格数据源
        /// </summary>
        /// <param name="condition">查询条件,为null或者空就是查询所有记录</param>
        private void BindGrid(string condition)
        {
            mainDt = bll.GetList(condition);//查询
            this.bindingSource1.DataSource = mainDt;
            this.dataGridView1.DataSource = this.bindingSource1;
            //加载表格样式
            Common.LoadGridStyleByXML(this.Name, dataGridView1);
        }
        /// <summary>
        /// 更新数据源数据
        /// </summary>
        private void SaveGrid()
        {                     
            StringBuilder opmsg = new StringBuilder();
            int result = bll.Update(mainDt);
            if (result > 0)
            {
                opmsg.AppendLine("保存成功");
            } else {
                opmsg.AppendLine("保存失败");
            }
            //弹出反馈消息
             MessageBox.Show(this, opmsg.ToString(), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {            
                DataGridView.HitTestInfo info = dataGridView1.HitTest(e.Location.X, e.Location.Y);
                if (info.ColumnIndex > -1)
                {
                    if (info.RowIndex > -1)
                        dataGridView1.CurrentCell = dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex];
                    Point point = this.PointToClient(new Point(e.X, e.Y));
                    Common.showContextMenu(dataGridView1, point, this.Name, false, false);
                }
            }
        }
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > -1 && e.RowIndex > -1)
            {
                string key = dataGridView1.Rows[e.RowIndex].Cells["ColumnID"].Value.ToString();
                
                if(dataGridView1.Columns[e.ColumnIndex].HeaderText=="持有人")
                {
                    //这个是查看当前持有人与所有的状态。 you're fired.你被解雇了，你被炒鱿鱼了。
                    DocumentDetail ff = new DocumentDetail(key);
                    ff.ShowDialog();
                    BindDataWithPage(1);
                    return;
                }
                

                using (docmgr.WinForm.DocumentOperation frm = new DocumentOperation(key))
                {
                    /*DataTable dt = bll.GetModel(key);
                    if(Convert.ToInt32(dt.Rows[0]["HandleUserID"])!=Convert.ToInt32(Common.curUser.Rows[0]["ID"]))
                    {//必须当前持有人 才可以 对其进行 更改
                        MessageBox.Show("非当前持有人禁止操作！");
                        return;

                    }*/

                    string sqlStr = String.Format("select count(*) c from DocHandler where DocID = {0} and HandlerUserID ={1}", key,Convert.ToInt32(Common.curUser.Rows[0]["ID"]));

                    int x=Convert.ToInt32(SQLHelper.ExecuteDataSet(SQLHelper.connString,sqlStr).Tables[0].Rows[0]["c"]);

                    if(x==0)
                    {
                        MessageBox.Show("非当前持有人禁止操作！");
                        return;
                    }

                    frm.WindowState = FormWindowState.Normal;
                    frm.StartPosition = FormStartPosition.CenterScreen;
                    if (frm.ShowDialog() == DialogResult.OK)
                    {
                        
                    }
                    BindDataWithPage(allenPage1.PageIndex);
                }



                /*using (docmgr.WinForm.DocumentForm frm = new docmgr.WinForm.DocumentForm(key))
                {
                    frm.WindowState = FormWindowState.Normal;
                    frm.StartPosition = FormStartPosition.CenterScreen;
                    if (frm.ShowDialog() == DialogResult.OK)
                    {
                        BindGrid(null);
                    }
                }*/
            }
        }
        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                dataGridView1.Rows[i].HeaderCell.Value = (i + 1).ToString();
            }
        }
        #endregion

        private void allenPage1_PageIndexChanged(object sender, EventArgs e)
        {
            BindDataWithPage(allenPage1.PageIndex);
        }

        private void BindDataWithPage(int Index)
        {
            allenPage1.PageIndex = Index;
            
            DataSet ds = bll.GetPage(fields, hidSQL, allenPage1.PageIndex, allenPage1.PageSize);


            DataTable dd = ds.Tables[0];

            DataColumn dc = new DataColumn("持有人");
            dd.Columns.Add(dc);

            DataColumn dstatus = new DataColumn("状态");

            dd.Columns.Add(dstatus);
            for (int i = 0; i < dd.Rows.Count; i++)
            {
                string key = Convert.ToString(dd.Rows[i]["ID"]);

                String sqlStr = "SELECT  u.name   from DocHandler as dh LEFT JOIN Document as doc on  dh.DocID=doc.id LEFT JOIN [User] as u on u.ID=dh.HandlerUserID where doc.id=" + key + " GROUP BY u.name ";
                sqlStr = "SELECT	p.c, u.name FROM  ( SELECT COUNT (*) c,  HandlerUserID AS h FROM DocHandler AS dh LEFT JOIN [User] AS u ON HandlerUserID = u.id WHERE 	dh.DocID ="+ key +" GROUP BY dh.HandlerUserID ) AS p LEFT JOIN [User] AS u ON u.ID = p.h";
                String HandlerU = "";
                DataTable d = SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlStr).Tables[0];

                for (int j = 0; j < d.Rows.Count; j++)
                {
                    if (j != 0)
                        HandlerU += ";";
                    HandlerU +=  d.Rows[j]["name"];
                    int count = Convert.ToInt32(d.Rows[j]["c"]);
                    if(count!=1)
                    {
                        HandlerU += "*" + count;
                    }
                }
                dd.Rows[i]["持有人"] = HandlerU;
            }
            for (int i = 0; i < dd.Rows.Count; i++)
            {
                string key = Convert.ToString(dd.Rows[i]["ID"]);

                String sqlStr = "SELECT  u.name   from DocHandler as dh LEFT JOIN Document as doc on  dh.DocID=doc.id LEFT JOIN [User] as u on u.ID=dh.HandlerUserID where doc.id=" + key + " GROUP BY u.name ";
                sqlStr = "  SELECT COUNT (*) c,  status   FROM DocHandler   WHERE 	 DocID =" + key + " GROUP BY status ";
                String HandlerU = "";
                DataTable d = SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlStr).Tables[0];

                for (int j = 0; j < d.Rows.Count; j++)
                {
                    if (j != 0)
                        HandlerU += ";";
                    HandlerU += d.Rows[j]["status"];
                    int count = Convert.ToInt32(d.Rows[j]["c"]);
                    if (count != 1)
                    {
                        HandlerU += "*" + count;
                    }
                }
                dd.Rows[i]["状态"] = HandlerU;
            }
            dataGridView1.DataSource = dd;

                //获取并设置总记录数
            allenPage1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]); 
        }

        private void BindDataWithPage(int Index,String condition)
        {
            allenPage1.PageIndex = Index;
            DataSet ds = bll.GetPage("* ", condition, allenPage1.PageIndex, allenPage1.PageSize);


            dataGridView1.DataSource = ds.Tables[0];

            //获取并设置总记录数
            allenPage1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            hidSQL = "";
            BindDataWithPage(1);
        }

        private void DocumentList_Resize(object sender, EventArgs e)
        {
           // dataGridView1.Size = new Size(panel1.Size.Width,panel1.Height);
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            fields = fields1;//总的，所有的。看起来不错 ,今天又来改，
            hidSQL = "";
            tsTBoxKey.Text = "";
            if(e.Node.Text=="所有文件")
            {
                BindDataWithPage(1);
                return;
            }

            if(e.Node.Parent.Text=="所有文件")
            {
                String con = "";
                int y = Convert.ToInt32(DateTime.Now.ToString().Substring(0,4))-4;
                try
                {
                    y=Convert.ToInt32(e.Node.Text);

                    //con = String.Format(" RegDate BETWEEN '{0}' and '{1}' ", new DateTime(y, 1, 1).ToString(), new DateTime(y + 1, 1, 1).ToString());

                    con = String.Format(" (   (HandleUserID={0}) OR  (HandleUserID<1950 AND RegDate BETWEEN '{1}' and '{2}')   ) ", y, new DateTime(y, 1, 1).ToString(), new DateTime(y + 1, 1, 1).ToString());
                }
                catch(Exception ex11)
                {
                    Console.WriteLine(ex11.StackTrace);

                    //con = String.Format(" RegDate < '{0}'", new DateTime(y, 1, 1));

                    con = String.Format(" (   ( HandleUserID>1950 and HandleUserID<={0}) OR  (HandleUserID<1950 AND RegDate< '{1}')   ) ", y, new DateTime(y, 1, 1).ToString());

                }
                hidSQL = con;
                BindDataWithPage(1);
                return;
            }

            else
            {
                String con = "";
                int y = Convert.ToInt32(DateTime.Now.ToString().Substring(0, 4)) - 4;

                try
                {
                    y = Convert.ToInt32(e.Node.Parent.Text);

                    //con = String.Format(" RegDate BETWEEN '{0}' and '{1}' ", new DateTime(y, 1, 1).ToString(), new DateTime(y + 1, 1, 1).ToString());
                    con = String.Format(" (   (HandleUserID={0}) OR  (HandleUserID<1950 AND RegDate BETWEEN '{1}' and '{2}')   ) ", y, new DateTime(y, 1, 1).ToString(), new DateTime(y + 1, 1, 1).ToString());
                }
                catch (Exception ex11)
                {
                    Console.WriteLine(ex11.StackTrace);

                    //con = String.Format(" RegDate < '{0}'", new DateTime(y, 1, 1));
                    con = String.Format(" (   ( HandleUserID>1950 and HandleUserID<={0}) OR  (HandleUserID<1950 AND RegDate< '{1}')   ) ", y, new DateTime(y, 1, 1).ToString());
                }

                con += String.Format(" and DocType= '{0}' ", e.Node.Text);

                if(e.Node.Text=="收文")
                {
                    fields = fields2;
                }
                else
                {
                    fields = fields3;
                }

                hidSQL = con;
                BindDataWithPage(1);
                return;
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void 全部ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            hidSQL = "";
            BindDataWithPage(1);
        }

        private void 销毁ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            

            String sqlStr = "SELECT DocID as ID FROM [dbo].[DocHandler] where Status='销毁' GROUP BY DocID";
            DataTable d = SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlStr).Tables[0];
            String idstr = "(";

            for (int j = 0; j < d.Rows.Count; j++)
            {
                if (j != 0)
                    idstr += ",";
                idstr += d.Rows[j]["ID"];
            }

            idstr += ")";

            if (d.Rows.Count != 0) hidSQL = "Doc.ID in " + idstr + " and  ";
            else
            {
                hidSQL = "";
                MessageBox.Show("销毁状态的文件为空");
            }

            string condition = "";//在这里设置查询条件,如 "title like '%good%'",为空就是查询所有记录
            //BindGrid(condition);//重新绑定表格
            if (dateBegin.Text != dateEnd.Text)
                condition += String.Format(" RegDate BETWEEN '{0}' and '{1}' ", Convert.ToDateTime(dateBegin.Text), Convert.ToDateTime(dateEnd.Text));
            else
                condition += "1=1";
            if (tsTBoxKey.Text != "")
            {
                /**全文搜索，就酱紫吧*/
                condition += String.Format(" and  (DocName like '%{0}%' or doc.Remark like '%{0}%' or u.name like '%{0}%'   or dep.name like '%{0}%'  or doc.DocSerialNumber like  '%{0}%'  or doc.SysSerialNumber like  '%{0}%' ) ", tsTBoxKey.Text);
     
            }
            hidSQL += condition;

            BindDataWithPage(1);//, hidSQL);
            allenPage1.Refresh();
        }

        private void 在库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String sqlStr = "SELECT DocID as ID FROM [dbo].[DocHandler] where Status='在库' GROUP BY DocID";
            DataTable d = SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlStr).Tables[0];
            String idstr = "(";

            for (int j = 0; j < d.Rows.Count; j++)
            {
                if (j != 0)
                    idstr += ",";
                idstr += d.Rows[j]["ID"];
            }

            idstr += ")";

            if (d.Rows.Count != 0) hidSQL = "Doc.ID in " + idstr + " and  ";
              else
            {
                hidSQL = "";
                MessageBox.Show("在库状态的文件为空");
            }

            string condition = "";//在这里设置查询条件,如 "title like '%good%'",为空就是查询所有记录
            //BindGrid(condition);//重新绑定表格
            if (dateBegin.Text != dateEnd.Text)
                condition += String.Format(" RegDate BETWEEN '{0}' and '{1}' ", Convert.ToDateTime(dateBegin.Text), Convert.ToDateTime(dateEnd.Text));
            else
                condition += "1=1";
            if (tsTBoxKey.Text != "")
            {
                /**全文搜索，就酱紫吧*/
                condition += String.Format(" and  (DocName like '%{0}%' or doc.Remark like '%{0}%' or u.name like '%{0}%'   or dep.name like '%{0}%'  or doc.DocSerialNumber like  '%{0}%'  or doc.SysSerialNumber like  '%{0}%' ) ", tsTBoxKey.Text);

            }
            hidSQL += condition;

            BindDataWithPage(1);//, hidSQL);
            allenPage1.Refresh();
        }

        private void 传阅ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String sqlStr = "SELECT DocID as ID FROM [dbo].[DocHandler] where Status='传阅' GROUP BY DocID";
            DataTable d = SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlStr).Tables[0];
            String idstr = "(";

            for (int j = 0; j < d.Rows.Count; j++)
            {
                if (j != 0)
                    idstr += ",";
                idstr += d.Rows[j]["ID"];
            }

            idstr += ")";

            if (d.Rows.Count != 0) hidSQL = "Doc.ID in " + idstr + " and  ";
            else
            {
                hidSQL = "";
                MessageBox.Show("传阅状态的文件为空");
            }
            string condition = "";//在这里设置查询条件,如 "title like '%good%'",为空就是查询所有记录
            //BindGrid(condition);//重新绑定表格
            if (dateBegin.Text != dateEnd.Text)
                condition += String.Format(" RegDate BETWEEN '{0}' and '{1}' ", Convert.ToDateTime(dateBegin.Text), Convert.ToDateTime(dateEnd.Text));
            else
                condition += "1=1";
            if (tsTBoxKey.Text != "")
            {
                /**全文搜索，就酱紫吧*/
                condition += String.Format(" and  (DocName like '%{0}%' or doc.Remark like '%{0}%' or u.name like '%{0}%'   or dep.name like '%{0}%'  or doc.DocSerialNumber like  '%{0}%'  or doc.SysSerialNumber like  '%{0}%' ) ", tsTBoxKey.Text);

            }
            hidSQL += condition;

            BindDataWithPage(1);//, hidSQL);
            allenPage1.Refresh();
        }

        private void 存档ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String sqlStr = "update [dbo].[DocHandler] set status ='存档'    where status='在库' AND SysSerialNumber!=''";
            SQLHelper.ExecuteSql(SQLHelper.connString, sqlStr);


            sqlStr = "SELECT DocID as ID FROM [dbo].[DocHandler] where Status='存档' GROUP BY DocID";
            DataTable d = SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlStr).Tables[0];
            String idstr = "(";

            for (int j = 0; j < d.Rows.Count; j++)
            {
                if (j != 0)
                    idstr += ",";
                idstr += d.Rows[j]["ID"];
            }

            idstr += ")";

            if (d.Rows.Count != 0) hidSQL = "Doc.ID in " + idstr + " and  ";
            else
            {
                hidSQL = "";
                MessageBox.Show("传阅状态的文件为空");
            }
            string condition = "";//在这里设置查询条件,如 "title like '%good%'",为空就是查询所有记录
            //BindGrid(condition);//重新绑定表格
            if (dateBegin.Text != dateEnd.Text)
                condition += String.Format(" RegDate BETWEEN '{0}' and '{1}' ", Convert.ToDateTime(dateBegin.Text), Convert.ToDateTime(dateEnd.Text));
            else
                condition += "1=1";
            if (tsTBoxKey.Text != "")
            {
                /**全文搜索，就酱紫吧*/
                condition += String.Format(" and  (DocName like '%{0}%' or doc.Remark like '%{0}%' or u.name like '%{0}%'   or dep.name like '%{0}%'  or doc.DocSerialNumber like  '%{0}%'  or doc.SysSerialNumber like  '%{0}%' ) ", tsTBoxKey.Text);

            }
            hidSQL += condition;

            BindDataWithPage(1);//, hidSQL);
            allenPage1.Refresh();
        }
    }
}